﻿@{
    ViewBag.Title = "Index";
}
<br />
<h2>NonFSA Email Templates</h2>
<br />
@(Html.Telerik().Grid<Century.Models.Core.EmailTemplate>()
    .Name("Grid")
    .DataKeys(keys => keys.Add(c => c.Id))
    .ToolBar(commands =>
        {
            commands.Template(
                @<text> <a href="#" class="t-grid-action t-button t-state-default t-grid-add">Add new
                    record</a>
<label for="Template-Select">
    Show templates for :
</label>
@(Html.Telerik().ComboBox()
                        .Name("cmbTemplates")
                        .DataBinding(binding => binding.Ajax().Select("GetTemplateNames", "EmailTemplateNonFSA"))
                        .AutoFill(true)
                        .Filterable(filtering =>
                        {
                            filtering.FilterMode(AutoCompleteFilterMode.StartsWith);
                        })
                        .HtmlAttributes(new { style = "width: 300px" })
                        .HighlightFirstMatch(true)
                        .ClientEvents(events => events.OnChange("templateChange"))
                    )
</text>);
            //commands.Insert().ButtonType(GridButtonType.ImageAndText).ImageHtmlAttributes(new { style = "margin-left:0" });
        })

    .Columns(columns =>
    {
        columns.Bound(o => o.Id).Hidden().ReadOnly(true);
        columns.ForeignKey(o => o.CountrySiteId, (System.Collections.IEnumerable)ViewBag.AccessibleSites, "Id", "Path");
        columns.Bound(o => o.Name);
        columns.Bound(o => o.Subject).Width(300);
        columns.Bound(o => o.Version).Width(50);
        columns.Bound(o => o.Body).EditorTemplateName("Editor").Hidden();
        columns.Command(command => { command.Edit().ButtonType(GridButtonType.Image); }).Width(50);
        //columns.Command(command =>
        //{
        //    command.Custom("Copy").Text("Copy").ButtonType(GridButtonType.Text)
        //        .DataRouteValues(route => route.Add(o => o.Id).RouteKey("Id")).Action("Copy", "EmailTemplateNonFSA");
        //}).Width(50);
    })
    .DetailView(details => details.ClientTemplate(
        Html.Telerik().Grid<Century.Models.Core.EmailFileType>()
            .DataKeys(keys => keys.Add(c => c.Id))
            .Name("EmailTemplateFiles_<#= Id #>")
            .Columns(columns =>
            {
                columns.Bound(o => o.Id);
                columns.Bound(o => o.Name);
                columns.Bound(o => o.MediaType);
                //columns.Command(command =>
                //{
                //    command.Edit().ButtonType(GridButtonType.ImageAndText);

                //}).Width(150);
            })
            .DataBinding(dataBinding => dataBinding.Ajax()
                              .Select("DetailsForEmailTemplate", "EmailTemplateNonFSA", new { templateId = "<#= Id #>" })
        //.Update("EditDetailsForConfig", "EmailTemplateFSA", new { configId = "<#= Id #>" })
                        )
                        .Pageable()
                        .Sortable()
                        .Filterable()
                        .Editable(editing => editing.Mode(GridEditMode.InForm))
                        .ToHtmlString()
     ))
    .ClientEvents(clientEvents =>
    {
        clientEvents.OnDataBinding("dataBinding");
        clientEvents.OnEdit("editClick");
    }
        )
    .DataBinding(dataBinding => dataBinding.Ajax()
        .Select("GetEmailTemplateList", "EmailTemplateNonFSA")
        .Update("EditEmailTemplate", "EmailTemplateNonFSA")
        .Insert("InsertEmailTemplate", "EmailTemplateNonFSA")
    )
    .Groupable()
    .Filterable()
    .Editable(editing => editing.Mode(GridEditMode.PopUp))
    .Sortable(sortable => sortable.OrderBy(order => order.Add(o => o.Name)))
    .Pageable(pagingSetting => pagingSetting.PageSize(20))
    .Resizable(config => { config.Columns(true); })
    .Reorderable(config => { config.Columns(true); })
    .ColumnContextMenu()
                        )
<script type="text/javascript">

    function templateChange() {
        $("#Grid").data("tGrid").rebind();
    }

    function dataBinding(args) {
        if ($("#cmbTemplates").data("tComboBox")) {
            var templateName = $("#cmbTemplates").data("tComboBox").value();
            args.data = $.extend(args.data, { templateName: templateName });
        }

    }

    function editClick(e) {
        $('.t-html').click('Body', showHtml);
        $(e.form).closest(".t-window-content").width(600).height(600);
        var popup = $("#" + e.currentTarget.id + "PopUp");
        popup.css({ "left": ($(window).width() / 2 - $(popup).width() / 2) + "px", "top": ($(window).height() / 2 - $(popup).height() / 2) + "px" });

        $('#Name')[0].readOnly = 'true';
        $('.editor-label:eq(3)').hide();
        $('#Version').hide();
        $('.editor-label:eq(4)').hide();
        $('.editor-field:eq(4)').hide();
    }

</script>
